﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using OLTS.DATA_SERVICE;
using OLTS.MODEL;
using OLTS.WEB.BaseClass;

namespace ONTS.WEB.Service
{
    /// <summary>
    /// Summary description for TimeSheetOverviewService
    /// </summary>
    public class TimeSheetOverviewService : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;

            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            var _cmd = new TimeSheet_Service(BaseService.LOGIN.EMPLOYEE_ID, Convert.ToInt16(request["MONTH"]));
            var list = new List<TIME_SHEET_OVERVIEW>();
            if (request["MONTH"] != null)
            {

                var lstTimesheet = _cmd.GetAllinclude();
                 
                foreach (var d in lstTimesheet)
                { 
                    foreach (var ch in d.OTS_WORK_DETAIL.ToList())
                    {
                        var obj = new TIME_SHEET_OVERVIEW();
                        obj.DATE = d.DATE;
                        obj.DETAIL = ch.DETAIL;
                        if (ch.START_TIME == "" || ch.START_TIME == null)
                        {
                            if (BaseService.PROJECT.OTS_SITE.TIMESHEET_FORMAT == "1")
                            {
                                obj.TIME = "-";
                            }
                            else
                            {
                                obj.TIME = "8:30-17:00";
                            }
                            
                        }
                        else
                        {
                            obj.TIME = ch.START_TIME + " - " + ch.END_TIME;
                        }
                        obj.HOURS = ch.AMOUNT_OF_TIME;
                        obj.OT = ch.AMOUNT_OF_TIME_OVER_TIME;
                        list.Add(obj);
                    }
                }

            }
            list = list.OrderByDescending(o => o.DATE).ToList();

            int total = list.Count == 0 ? 1 : (int)Math.Ceiling((double)list.Count / 10);

            var jsonData = new
            {
                total = total,
                page = request["page"],
                records = list.Count,
                rows = list.Skip(Convert.ToInt16(request["page"]) * 10 - 10).Take(10)
            };

            response.Write(new JavaScriptSerializer().Serialize(jsonData));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}